﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public enum TPRProcess
    { 
        NotBegin = 0,
        BeginToZero = 1,
        BeginToStartPosition = 2,
        Executing = 10,
        EndToStartPosition = 20
    }

    public class TPRState
    {
        public TPRState(int planID)
        {
             m_PlanID = planID;
             m_Process = TPRProcess.NotBegin;
             m_TotalStep = 0;
             m_InnerStep = 0;
             m_InnerStepLoop = 0;
        }

        public void setAxleList(List<string> axleList)
        {
            bool inited = true;
            if (m_AxleRunToZero == null || m_AxleRunToZero.Count == 0)
            {
                m_AxleRunToZero = new Dictionary<string, bool>();
                inited = false;
            }
            if (m_AxleRunToPosition == null || m_AxleRunToPosition.Count == 0)
            {
                m_AxleRunToPosition = new Dictionary<string, bool>();
                inited = false;
            }
            if (!inited && axleList != null)
            {
                foreach (string aName in axleList)
                {
                    if (!m_AxleRunToZero.ContainsKey(aName))
                    {
                        m_AxleRunToZero.Add(aName, false);
                    }
                    if (!m_AxleRunToPosition.ContainsKey(aName))
                    {
                        m_AxleRunToPosition.Add(aName, false);
                    }
                }
            }
        }

        public void setState(TPRProcess process)
        {
            setState(process, 0, 0, 0);
        }

        public void setState(
             int totalStep
            , int innerStep
            , int innerSteploop
         )
        {
            setState(TPRProcess.Executing, totalStep, innerStep, innerSteploop);
        }

        public void setState(TPRProcess process
            , int totalStep
            , int innerStep
            , int innerSteploop
         )
        {
            m_Process = process;
            m_TotalStep = totalStep;
            m_InnerStep = innerStep;
            m_InnerStepLoop = innerSteploop;
        }

        private Dictionary<string, bool> m_AxleRunToPosition;
        public bool this[string axleName]
        {
            get
            {
                if (m_AxleRunToPosition == null) { m_AxleRunToPosition = new Dictionary<string, bool>(); }
                if (m_AxleRunToPosition.ContainsKey(axleName))
                {
                    return m_AxleRunToPosition[axleName];
                }
                return false;
            }
            set
            {
                if (m_AxleRunToPosition == null) { m_AxleRunToPosition = new Dictionary<string, bool>(); }
                if (m_AxleRunToPosition.ContainsKey(axleName) && m_AxleRunToPosition[axleName] != value)
                {
                    m_AxleRunToPosition[axleName] = value;
                }
            }
        }

        private Dictionary<string, bool> m_AxleRunToZero;
        public bool getToZeroState(string axleName)
        {

            if (m_AxleRunToZero == null) { m_AxleRunToZero = new Dictionary<string, bool>(); }
            if (m_AxleRunToZero.ContainsKey(axleName))
            {
                return m_AxleRunToZero[axleName];
            }
            return false;


        }
        public void setToZeroState(string axleName, bool bValue)
        {

            if (m_AxleRunToZero == null) { m_AxleRunToZero = new Dictionary<string, bool>(); }
            if (m_AxleRunToZero.ContainsKey(axleName) && m_AxleRunToZero[axleName] != bValue)
            {
                m_AxleRunToZero[axleName] = bValue;
            }

        }

        public bool NoToZeroReady
        {
            get
            {
                bool ready = true;
                if (m_AxleRunToZero != null && m_AxleRunToZero.Count > 0)
                {
                    foreach (bool bvalue in m_AxleRunToZero.Values)
                    {
                        if (!bvalue) return true;
                        ready &= bvalue;

                    }
                }

                return !ready;
            }
        }

        public bool NoToPositionReady
        {
            get
            {
                bool ready = true;
                if (m_AxleRunToPosition != null && m_AxleRunToPosition.Count > 0)
                {
                    foreach (bool bvalue in m_AxleRunToPosition.Values)
                    {
                        if (!bvalue) return true;
                        ready &= bvalue;

                    }
                }

                return !ready;
            }
        }

        public bool NoInitReady
        {
            get
            {
                //bool ready = true;
                //ready &= NoToZeroReady;
                //ready &= NoToPositionReady;
                //return ready;
                return NoToZeroReady || NoToPositionReady;
            }

        }

        public void Reset()
        {
            setState(TPRProcess.NotBegin);

            if (m_AxleRunToZero != null && m_AxleRunToZero.Count > 0)
            {
                //foreach (string bkey in m_AxleRunToZero.Keys)
                //{
                //    m_AxleRunToZero[bkey] = false;

                //}
                m_AxleRunToZero.Clear();
            }

            if (m_AxleRunToPosition != null && m_AxleRunToPosition.Count > 0)
            {
                //foreach (string bkey in m_AxleRunToPosition.Keys)
                //{
                //    m_AxleRunToPosition[bkey] = false;

                //}
                m_AxleRunToPosition.Clear();
            }
        }


        int m_PlanID = 0;

        public int PlanID
        {
            get { return m_PlanID; }
            //set { m_PlanID = value; }
        }
        TPRProcess m_Process;

        public TPRProcess Process
        {
            get { return m_Process; }
            //set { m_Process = value; }
        }
        int m_TotalStep = 0;

        public int TotalStep
        {
            get { return m_TotalStep; }
            //set { m_TotalStep = value; }
        }
        int m_InnerStep = 0;

        public int InnerStep
        {
            get { return m_InnerStep; }
            //set { m_InnerStep = value; }
        }

        int m_InnerStepLoop = 0;

        public int InnerStepLoop
        {
            get { return m_InnerStepLoop; }
            //set { m_InnerStepLoop = value; }
        }
    }

    public class TPRStateManager
    { 
        protected TPRStateManager()
        {
            m_Dict = new Dictionary<int,TPRState>();
        }

        //public void Add(TPRState state)
        //{
        //    if (!m_Dict.ContainsKey(state.PlanID))
        //    {
        //        m_Dict.Add(state.PlanID, state);
        //    }
        //}

        private static TPRStateManager m_instance;
        public static TPRStateManager Instance
        {
            get{
                if(m_instance == null)
                {
                    m_instance = new TPRStateManager();
                }
                return m_instance;
            }
        }



        private Dictionary<int,TPRState> m_Dict ;
        public TPRState this[int pId]
        {
            get{
                if(m_Dict == null){m_Dict = new Dictionary<int,TPRState>();}
                if(m_Dict.ContainsKey(pId))
                {
                    return m_Dict[pId];
                }
                return null;
            }
        }

        public void Delete(int pId)
        {
            if (m_Dict.ContainsKey(pId))
            {
                m_Dict.Remove(pId);
            }
        }

        public void Update(int pId)
        {
            if (m_Dict.ContainsKey(pId))
            {
                m_Dict[pId].Reset();
            }
            else
            {
                m_Dict.Add(pId, new TPRState(pId));
            }
        }
    }
}
